package com.jus.player.fragment.home;

import static com.jus.player.api.OkHttpHelper._DATA;

import android.annotation.SuppressLint;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSONObject;
import com.jus.player.R;
import com.jus.player.adapter.home.FollowTvAdaptor;
import com.jus.player.api.ApiConfig;
import com.jus.player.api.OkHttpHelper;
import com.jus.player.entity.SpacesItemDecoration;
import com.jus.player.entity.VideoEntity;
import com.jus.player.fragment.BaseFragment;
import com.jus.player.util.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 追剧页面
 */
public class SubFollowFragment extends BaseFragment {

    private FollowTvAdaptor adapter;
    private final List<VideoEntity> dataList = new ArrayList<>();
    private RecyclerView recyclerView;

    private View emptyStateView;

    public static SubFollowFragment newInstance() {
        return new SubFollowFragment();
    }

    boolean isFirst = false;

    @Override
    protected int initLayout() {
        return R.layout.fragment_sub_follow;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.grid_recyclerView);
        int spanCount = 3;
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), spanCount));
        //设置间隙装饰
        recyclerView.addItemDecoration(
                new SpacesItemDecoration(spanCount, 20, false));
        adapter = new FollowTvAdaptor();
        adapter.setVideos(dataList);
        recyclerView.setAdapter(adapter);
        //空页面
        emptyStateView = findViewById(R.id.layoutEmptyState1);
    }

    @Override
    protected void initData() {
        //请求数据
        isFirst = true;
        getDataVideos();
    }

    @Override
    protected boolean isLazyLoad() {
        return true;
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isFirst) {
            getDataVideos();
        }
    }

    private void getDataVideos() {
        dataList.clear();
        recyclerView.setVisibility(View.VISIBLE);
        emptyStateView.setVisibility(View.GONE);
        //查询追剧集合
        HashMap<String, String> map = new HashMap<>();
        map.put("type", "0");
        OkHttpHelper.getInstance(requireActivity()).getAsync(ApiConfig.FOLLOW_TV_LIST, map,
                new OkHttpHelper.HttpCallback() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void onSuccess(String res) {
                        JSONObject jo = JSONObject.parseObject(res);
                        List<VideoEntity> resList = jo.getList(_DATA, VideoEntity.class);
                        if (resList != null) {
                            dataList.addAll(resList);
                        }
                        if (getActivity() != null) {
                            getActivity().runOnUiThread(() -> adapter.notifyDataSetChanged());
                        }
                        if (resList == null || resList.isEmpty()) {
                            getActivity().runOnUiThread(() -> {
                                // 没有数据时显示空状态页面，隐藏列表
                                recyclerView.setVisibility(View.GONE);
                                emptyStateView.setVisibility(View.VISIBLE);
                            });
                        }
                        isFirst = false;
                    }

                    @Override
                    public void onFailure(OkHttpHelper.HttpException e) {
                        CommonUtils.showToastAsync(requireActivity(), e.getMessage());
                    }
                });
    }

}